home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / Portscan.php < prev    next >
PHP Script  |  2004-03-24  |  4KB  |  127 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Portscan.php,v 1.5 2003/04/22 12:14:11 mj Exp $
  21.  
  22. define("NET_PORTSCAN_SERVICE_FOUND", true);
  23. define("NET_PORTSCAN_NO_SERVICE", false);
  24.  
  25. /**
  26.  * Portscan class
  27.  *
  28.  * This class provides methods to scan ports on machines,
  29.  * that are connected to the internet. See README for more
  30.  * information on how to use it.
  31.  *
  32.  * @author  Martin Jansen <mj@php.net>
  33.  * @package Net_Portscan
  34.  * @category Net
  35.  * @version $Revision: 1.5 $
  36.  */
  37. class Net_Portscan
  38. {
  39.     // {{{ checkPort()
  40.  
  41.     /**
  42.      * Check if there is a service available at a certain port.
  43.      *
  44.      * This function tries to open a connection to the port
  45.      * $port on the machine $host. If the connection can be
  46.      * established, there is a service listening on the port.
  47.      * If the connection fails, there is no service.
  48.      *
  49.      * @access public
  50.      * @param  string  Hostname
  51.      * @param  integer Portnumber
  52.      * @param  integer Timeout for socket connection in seconds (default is 30).
  53.      * @return string
  54.      */
  55.     function checkPort($host, $port, $timeout = 30)
  56.     {
  57.         $socket = @fsockopen($host, $port, $errorNumber, $errorString, $timeout);
  58.  
  59.         if (!$socket) {
  60.             return NET_PORTSCAN_NO_SERVICE;
  61.         }
  62.  
  63.         @fclose($socket);
  64.         return NET_PORTSCAN_SERVICE_FOUND;
  65.     }
  66.  
  67.     // }}}
  68.     // {{{ checkPortRange()
  69.  
  70.     /**
  71.      * Check a range of ports at a machine
  72.      *
  73.      * This function can scan a range of ports (from $minPort
  74.      * to $maxPort) on the machine $host for running services.
  75.      *
  76.      * @access public
  77.      * @param  string Hostname
  78.      * @param  integer Lowest port
  79.      * @param  integer Highest port
  80.      * @param  integer Timeout for socket connection in seconds (default is 30).
  81.      * @return array  Associative array containing the result
  82.      */
  83.     function checkPortRange($host, $minPort, $maxPort, $timeout = 30)
  84.     {
  85.         for ($i = $minPort; $i <= $maxPort; $i++) {
  86.             $retVal[$i] = Net_Portscan::checkPort($host, $i, $timeout);
  87.         }
  88.  
  89.         return $retVal;
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ getService()
  94.     
  95.     /**
  96.      * Get name of the service that is listening on a certain port.
  97.      *
  98.      * @access public
  99.      * @param  integer Portnumber
  100.      * @param  string  Protocol (Is either tcp or udp. Default is tcp.)
  101.      * @return string  Name of the Internet service associated with $service
  102.      */
  103.     function getService($port, $protocol = "tcp")
  104.     {
  105.         return @getservbyport($port, $protocol);
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ getPort()
  110.  
  111.     /**
  112.      * Get port that a certain service uses.
  113.      *
  114.      * @access public
  115.      * @param  string  Name of the service
  116.      * @param  string  Protocol (Is either tcp or udp. Default is tcp.)
  117.      * @return integer Internet port which corresponds to $service
  118.      */
  119.     function getPort($service, $protocol = "tcp")
  120.     {
  121.         return @getservbyname($service, $protocol);
  122.     }
  123.  
  124.     // }}}
  125. }
  126. ?>
  127.